package com.leenmvc.core.utils.word.wordUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 导出word文档
 * @Author  Cloud
 */
public class ExportWord {
    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;
    public ExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }
    /**
     * 创建好文档的基本 标题，表格  段落等部分
     * @return
     */
    public XWPFDocument createXWPFDocument() {
        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc);
        createNormalParagraph(doc,ParagraphAlignment.LEFT);
        createTableParagraph(doc, 34, 6);
        return doc;
    }
    /**
     * 创建表格的标题样式
     * @param document
     */
    public void createTitleParagraph(XWPFDocument document) {
        XWPFParagraph titleParagraph = document.createParagraph();    //新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
        XWPFRun titleFun = titleParagraph.createRun();    //创建文本对象
        titleFun.setBold(true); //加粗
        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(25);    //字体大小
    }


    /**
     * 创建文本段落样式
     * @Author Cloud
     * */
    public void createNormalParagraph(XWPFDocument document,ParagraphAlignment paragraphAlignment){
        XWPFParagraph normalParagraph = document.createParagraph();  //新建一个标题段落对象（就是一段文字）
        normalParagraph.setAlignment(paragraphAlignment);
        XWPFRun normalFun = normalParagraph.createRun();
        normalFun.setBold(false);
        normalFun.setFontSize(12);
        normalFun.setColor("000000");
    }
    /**
     * 设置表格
     * @param document
     * @param rows
     * @param cols
     * @Author Huangxiaocong 2018年12月16日
     */
    public void createTableParagraph(XWPFDocument document, int rows, int cols) {
        XWPFTable infoTable = document.createTable(rows, cols);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "9072", STJc.CENTER);
        //合并表格
        xwpfHelperTable.mergeCellsHorizontal(infoTable,4,0,5);

        xwpfHelperTable.mergeCellsHorizontal(infoTable,5,0,5);
        xwpfHelperTable.mergeCellsHorizontal(infoTable,17,0,5);

        xwpfHelperTable.mergeCellsHorizontal(infoTable,29,0,5);

        xwpfHelperTable.mergeCellsHorizontal(infoTable,31,1,5);
        xwpfHelperTable.mergeCellsHorizontal(infoTable,32,1,5);
        xwpfHelperTable.mergeCellsHorizontal(infoTable,33,1,5);
        xwpfHelperTable.mergeCellsVertically(infoTable,0,31,33);
        xwpfHelperTable.mergeCellsVertically(infoTable,1,31,33);

        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for(int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            List<XWPFTableCell> cellList = infoTableRow.getTableCells();
            for(int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(12);
            }
        }
        xwpfHelperTable.setTableHeight(infoTable, 560, STVerticalJc.CENTER);
    }

    /**
     * 往表格中填充数据
     * @param dataList
     * @param document
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public void exportCheckWord(HttpServletResponse response, Map<String, Object> dataList, XWPFDocument document, String savePath) throws IOException {
        XWPFParagraph title = document.getParagraphArray(0);
        XWPFRun titleFun = title.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));
        titleFun.addCarriageReturn();
        titleFun.setText(String.valueOf(dataList.get("TITLE1")));
        XWPFParagraph paragraph = document.getParagraphArray(1);
        XWPFRun textRun = paragraph.getRuns().get(0);
        textRun.setText(String.valueOf(dataList.get("text")));
        for (int i = 0; i < 3; i++) {
            textRun.addTab();
        }
        textRun.setText(String.valueOf(dataList.get("text1")));
        XWPFParagraph paragraph1 = document.getParagraphArray(1);
        XWPFRun textRun1 = paragraph1.getRuns().get(0);
        for (int i = 0; i < 4; i++) {
            textRun1.addTab();
        }
        textRun1.setText(dataList.get("text2").toString());
        List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
        XWPFTable table = document.getTableArray(0);
        fillTableData(table, tableData);
//        xwpfHelper.saveDocument(document, savePath);
        response.setCharacterEncoding("UTF-8");
//        response.setHeader("content-Type", "application/msword");
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(savePath, "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        document.write(out);
        out.flush();
    }

    /**
     * 往表格中填充数据
     * @param table
     * @param tableData
     */
    public void fillTableData(XWPFTable table, List<List<Object>> tableData) {
        List<XWPFTableRow> rowList = table.getRows();
        for(int i = 0; i < tableData.size(); i++) {
            List<Object> list = tableData.get(i);
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            for(int j = 0; j < list.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                cellParagraphRun.setText(String.valueOf(list.get(j)));
            }
        }
    }
}
